package com.kx.center.controller;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.kx.center.auth.Authentication;
import com.kx.center.entity.Account;
import com.kx.center.entity.StoreAppAccount;
import com.kx.center.entity.Suggestion;
import com.kx.center.enumerate.MessageCode;
import com.kx.center.exception.EmptyTokenException;
import com.kx.center.exception.IllegalTokenException;
import com.kx.center.service.SuggestionService;
import com.kx.center.util.JSONParser;
import com.kx.center.util.Response;
import com.kx.center.util.StringUtil;

/**
 * 意见反馈控制器
 * @description 
 * @version 1.0
 * @author Scott 
 * @date 2016年1月19日 上午9:51:30
 */
@Controller
@RequestMapping("suggestion")
public class SuggestionController extends CommonController{
	
	@Autowired
	SuggestionService suggestionService;

	/*
	 * app前端提交意见反馈接口
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST ,produces = "application/json;charset=UTF-8")
	@ResponseBody
	@Authentication
	public String add(HttpServletRequest request) throws EmptyTokenException, IllegalTokenException{
		String content = request.getParameter("content");
		
		StoreAppAccount appAccount = this.getStoreAppAccount(request);
		int storeId = appAccount.getStoreId();
		int accountId = appAccount.getId();
		int companyId = appAccount.getCompanyId();
		String userId = appAccount.getUserId();
		
		
		if(storeId <= 0 || accountId <= 0 || companyId <= 0 || StringUtils.isEmpty(content)){
			return Response.error(MessageCode.PARAMETER_WRONG);
		}
		
		Suggestion suggestion = new Suggestion();
		suggestion.setAccoutId(accountId);
		suggestion.setStoreId(storeId);
		suggestion.setCompanyId(companyId);
		suggestion.setContent(content);
		suggestion.setUserId(userId);
		
		suggestionService.addSuggestion(request, suggestion);
		return Response.success("添加成功");
	}
	
	/*
	 * 查询意见反馈列表
	 */
	@RequestMapping(value = "/frontList", method = RequestMethod.GET ,produces = "application/json;charset=UTF-8")
	@ResponseBody
	@Authentication
	public String userSuggestionList(HttpServletRequest request) throws EmptyTokenException, IllegalTokenException{
		
		StoreAppAccount appAccount = this.getStoreAppAccount(request);
		 int accountId = appAccount.getId();
		
		if(accountId <= 0){
			return Response.error(MessageCode.PARAMETER_WRONG);
		}
		List<Suggestion> suggestionList = suggestionService.findFrontList(accountId);
				
		HashMap<String, Object> result = new HashMap<>();
		result.put("datas", suggestionList);
		
		return Response.success(result);
	}
	
	/*
	 * 前端查询意见反馈详情
	 */
	@RequestMapping(value = "/frontDetail/{id}", method = RequestMethod.GET ,produces = "application/json;charset=UTF-8")
	@ResponseBody
	@Authentication
	public String userSuggestionDetail(HttpServletRequest request ,@PathVariable("id")String id){
		if(!NumberUtils.isDigits(id)){
			return Response.error(MessageCode.PARAMETER_WRONG);
		}
		
		Map<String,Object> result = suggestionService.findFrontDetail(NumberUtils.toInt(id));
		
		HashMap<String, Object> data = new HashMap<>();
		data.put("data", result);
		
		return Response.success(data);
	}
	
	/*
	 * 取消意见反馈
	 */
	@RequestMapping(value = "/cancel/{id}", method = RequestMethod.GET ,produces = "application/json;charset=UTF-8")
	@ResponseBody
	@Authentication
	public String cancel(HttpServletRequest request ,@PathVariable("id")String id){
		if(!NumberUtils.isDigits(id)){
			return Response.error(MessageCode.PARAMETER_WRONG);
		}
		suggestionService.cancelSuggest(NumberUtils.toInt(id));
		return Response.success("取消成功");
	}
	
	/*
	 * web后台获取意见反馈列表
	 */
	@RequestMapping(value = "/list/{pageNow}/{pageSize}", method = RequestMethod.GET ,produces = "application/json;charset=UTF-8")
	@ResponseBody
	@Authentication
	public String list(HttpServletRequest request ,@PathVariable("pageNow")String pageNow ,@PathVariable("pageSize")String pageSize){
		
		if(!NumberUtils.isDigits(pageNow) || !NumberUtils.isDigits(pageSize)){
			return Response.error(MessageCode.PARAMETER_WRONG);
		}
		
		Account account = this.getCommonUserAccount(request);
		if(account == null || account.getCompanyId() <=0 ){
			return Response.error(MessageCode.PARAMETER_WRONG);
		}

		List<Suggestion> suggestionList = suggestionService.findListByPage(account.getCompanyId(), NumberUtils.toInt(pageNow), NumberUtils.toInt(pageSize));
		
		//查询总共有多少条
		int pageCount = suggestionService.findPageCount(account.getCompanyId());
		
		HashMap<String, Object> map = new HashMap<>();
		map.put("datas", suggestionList);
		map.put("pageNow", pageNow);
		map.put("pageSize", pageSize);
		map.put("pageCount", pageCount);
		return Response.success(map);
	}
	
	/*
	 * 根据意见列表id查详情
	 */
	@RequestMapping(value = "/detail/{suggestionId}", method = RequestMethod.GET ,produces = "application/json;charset=UTF-8")
	@ResponseBody
	@Authentication
	public String detail(HttpServletRequest request ,@PathVariable("suggestionId")String suggestionId){
		if(!NumberUtils.isDigits(suggestionId)){
			return Response.error(MessageCode.PARAMETER_WRONG);
		}
		
		HashMap<String,Object> detail = (HashMap<String, Object>) suggestionService.findSuggestionDetail(NumberUtils.toInt(suggestionId));
		
		HashMap<String, Object> result = new HashMap<>();
		result.put("data", detail);
		
		return Response.success(result);
	}
	
	/*
	 * 提交回复意见
	 */
	@RequestMapping(value = "/reply", method = RequestMethod.POST ,produces = "application/json;charset=UTF-8")
	@ResponseBody
	@Authentication
	public String reply(HttpServletRequest request){
		
		String idStr = request.getParameter("id");
		String accoutIdStr = request.getParameter("account_id");
		String replyContent = request.getParameter("replay_content");
		int operatorId = this.getCommonUserAccount(request).getId();
		
		suggestionService.replySuggestion(NumberUtils.toInt(idStr),replyContent,operatorId ,NumberUtils.toInt(accoutIdStr));
		
		return Response.success("回复成功");
	}
	
}






